﻿using DongFangGuoKai.WMSPlatform.Commn.TokenModule.Models;
using DongFangGuoKai.WMSPlatform.Commn.TokenModule;
using DongFangGuoKai.WMSPlatform.Domain.Model;
using DongFangGuoKai.WMSPlatform.Service.User;
using DongFangGuoKai.WMSPlatform.Service.User.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : BaseController
    {
        private readonly IUserService userService;
        private readonly IConfiguration configuration;

        public UserController(IUserService userService, IConfiguration configuration)
        {
            this.userService = userService;
            this.configuration = configuration;
        }

        [HttpPost]
        public IActionResult Login(LoginPDADTO loginPDADTO)
        {
           
            var obj = userService.GetLogin(loginPDADTO);
            if (obj != null)
            {
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                
                //返回一个空的密码
                obj.Pwd = string.Empty;
                obj.Token = GetToken(obj.Id, obj.UserName);
                return Success(obj, "登录成功！");
            }
           
            return Failed("账号密码有误");
        }
        private string GetToken(int userId, string? userName)
        {
            var tokenModel = configuration.GetSection("Jwt").Get<JwtTokenModel>();
            tokenModel.UserId = userId;
            tokenModel.UserName = userName;
            return TokenHelper.CreateToken(tokenModel);

        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUser(UserRoleDto m)
        {
            bool IsOK = userService.AddUser(m);
            if (IsOK)
            {
                return Success("添加成功");
            }
            return Failed("添加失败");
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="uname">用户名</param>
        /// <param name="rid">角色Id</param>
        /// <param name="state">状态</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserList(string? uname = "", int rid = 0, int state = 3, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Failed( "分页条件有误！",400);
            }

            int totalCount;
            var list = userService.GetUserList(uname, rid, state, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Success(list,totalCount, "获取成功!");
            }
            return Failed("获取失败!");
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelUser(int uid)
        {
            if (uid <= 0)
            {
                return Failed( "Id有误！",400);
            }
            bool IsOK = userService.DelUser(uid);
            if (IsOK)
            {
                return Success( "删除成功！");
            }
            return Failed( "删除失败！");
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ulist"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DelUsers(List<TUsers> ulist)
        {
            if (ulist.Count == 0)
            {
                return Failed("删除失败！数据不能为空！",400);
            }
            bool rule = userService.DelUsers(ulist);
            if (rule)
            {
                return Success( "删除成功");
            }
            return Failed("删除失败");
        }

        /// <summary>
        /// 根据主键获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult URById(int uid)
        {
            if (uid <= 0)
            {
                return Failed("Id有误！", 400);
            }
            var obj = userService.URById(uid);
            if (obj != null)
            {
                return Success(obj, "获取成功");
            }
            return Failed( "获取失败！");
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdUser(UserRoleDto m)
        {
            if (m.Id <= 0)
            {
                return Failed("Id有误！", 400);
            }
            bool IsOK = userService.UpdUser(m);
            if (IsOK)
            {
                return Success("修改成功!");
            }
            return Failed("修改失败!");
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UserResetPwd(ResetPwdDto m)
        {
            if (m.uid <= 0)
            {
                return Failed("Id有误！", 400);
            }
            bool IsOK = userService.UserResetPwd(m);
            if (IsOK)
            {
                return Success("重置密码成功!");
            }
            return Failed("重置密码失败!");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UserModifyPwd(ModifyPwdDto m)
        {
            if (string.IsNullOrEmpty(m.NewPwd))
            {
                return Failed( "新密码不能为空！",400);
            }
            bool IsOK = userService.UserModifyPwd(m);
            if (IsOK)
            {
                return Success("修改成功！");
            }
            return Failed("修改失败！");
        }

        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdUserStatus(UpdStatusDto m)
        {
            if (m.Id <= 0)
            {
                return Failed("Id有误！", 400);
            }
            bool rule = userService.UpdUserStatus(m);
            if (rule)
            {
                return Success("修改成功");
            }
            return Failed( "修改失败");
        }
    }
}
